package com.ywd.materials.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywd.materials.admin.common.Common;
import com.ywd.materials.admin.common.CommonGoods;
import com.ywd.materials.admin.context.UserContext;
import com.ywd.materials.admin.dto.MaterialsGoodsDto;
import com.ywd.materials.admin.dto.query.MaterialsGoodsQuery;
import com.ywd.materials.admin.entity.MaterialsGoodsEntity;
import com.ywd.materials.admin.exception.CommonException;
import com.ywd.materials.admin.exception.RPage;
import com.ywd.materials.admin.mapper.MaterialsGoodsMapper;
import com.ywd.materials.admin.vo.AdminUserDetailsVo;
import com.ywd.materials.admin.vo.AdminUserVo;
import com.ywd.materials.admin.vo.MaterialsGoodsVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:lk
 * @Date: 2025/11/13  00:24
 */
@AllArgsConstructor
@Service
public class MaterialsGoodsServiceImpl extends ServiceImpl<MaterialsGoodsMapper, MaterialsGoodsEntity> {


    public Boolean saveGoods(MaterialsGoodsDto dto) {
        AdminUserVo user = UserContext.getUserOne();
        dto.setCreator(user.getId());
        return save(BeanUtil.copyProperties(dto, MaterialsGoodsEntity.class));
    }

    public Boolean updateGoods(MaterialsGoodsDto dto) {
        AdminUserVo user = UserContext.getUserOne();
        dto.setUpdater(user.getId());
        return updateById(BeanUtil.copyProperties(dto, MaterialsGoodsEntity.class));
    }

    public Boolean deleteGoods(Long id) {
        //物资是否在使用中
        if(false) throw new  CommonException("该物资正在使用中, 删除失败");
        return removeById(id);
    }

    public RPage<List<MaterialsGoodsVo>> getGoodsPage(MaterialsGoodsQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPage(), query.getLimit());
        List<MaterialsGoodsVo> list = this.baseMapper.getGoodsPage(query);
        return RPage.ok(list,query.getPage(), (query.getLimit()),page.getTotal());
    }

    public MaterialsGoodsVo getGoodsDetails(Long id) {
        return this.baseMapper.getGoodsDetails(id);
    }

    public List<CommonGoods> getGoodsPartList(String name) {
        LambdaQueryWrapper<MaterialsGoodsEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MaterialsGoodsEntity::getDeleted,0)
                .like(ObjectUtil.isNotEmpty(name),MaterialsGoodsEntity::getName,name);
        return BeanUtil.copyToList(baseMapper.selectList(queryWrapper), CommonGoods.class);
    }


    public List<MaterialsGoodsEntity> getByGoodsTypeId(Long goodsTypeId) {
        LambdaQueryWrapper<MaterialsGoodsEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MaterialsGoodsEntity::getDeleted,0)
                .eq(MaterialsGoodsEntity::getMaterialsGoodsTypeId,goodsTypeId);
        return baseMapper.selectList(queryWrapper);
    }


}
